package chapter16.tankwar;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @Auther: Tx
 * @Date: 2022/5/28 18:14
 */
public class DrawTank extends JFrame {

    private MyPanel myPanel = null;

    public static void main(String[] args) {
        new DrawTank();
    }

    public DrawTank() {
        myPanel = new MyPanel();
//         添加到窗口
        this.add(myPanel);
//         新建窗口
        this.setSize(1000, 750);
//        设置关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        显示
        this.setVisible(true);
//        监听键盘事件
        this.addKeyListener(myPanel);

    }
}

//为了监听键盘事件，实现keyListener
class MyPanel extends JPanel implements KeyListener {
    myTank myTank = null;//初始化自己的坦克
    Vector<EnemyTank>  enemyTanks = new Vector<>();//敌人坦克
    int enemyTankNum = 3;
    public MyPanel() {
        myTank = new myTank(500, 500);
        for (int i = 0; i < enemyTankNum; i++) {
            EnemyTank enemyTank = new EnemyTank(100 * (i + 1), 100);
            enemyTank.setDirection(2);
            enemyTanks.add(enemyTank);
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        drawTank(myTank.getX(), myTank.getY(), g, myTank.getDirection(),0);
        for (int i = 0; i < enemyTanks.size();i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            drawTank(enemyTank.getX(),enemyTank.getY(),g,enemyTank.getDirection(),1);
        }
    }

    public void drawTank(int x, int y, Graphics g, int direct, int type) {
        switch (type) {
            case 0:
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }
        //根据坦克方向，来绘制坦克
        switch (direct) {
            case 0://绘制向上的坦克
                g.fill3DRect(x, y, 10, 60, false);//画左边的轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//画左边的轮子
                g.fill3DRect(x + 10, y + 15, 20, 30, false);
                g.fillOval(x + 10, y + 20, 20, 20);//坦克的盖子
                g.drawLine(x + 20, y + 30, x + 20, y);//坦克的炮筒
                repaint();
                break;
            case 1://绘制向右的坦克
                g.fill3DRect(x, y, 60, 10, false);//画上边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//画下边的轮子
                g.fill3DRect(x + 15, y + 10, 30, 20, false);//坦克的中间部分
                g.fillOval(x + 20, y + 10, 20, 20);//坦克的盖子
                g.drawLine(x + 30, y + 20, x + 60, y + 20);//坦克的炮筒

                break;
            case 2://绘制向下的坦克
                g.fill3DRect(x, y, 10, 60, false);//画左边的轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//画左边的轮子
                g.fill3DRect(x + 10, y + 15, 20, 30, false);
                g.fillOval(x + 10, y + 20, 20, 20);//坦克的盖子
                g.drawLine(x + 20, y + 30, x + 20, y + 60);//坦克的炮筒
                break;
            case 3:
                g.fill3DRect(x, y, 60, 10, false);//画上边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//画下边的轮子
                g.fill3DRect(x + 15, y + 10, 30, 20, false);//坦克的中间部分
                g.fillOval(x + 20, y + 10, 20, 20);//坦克的盖子
                g.drawLine(x + 30, y + 20, x, y + 20);//坦克的炮筒
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            myTank.setDirection(0);
            //修改坦克的坐标
            myTank.moveUp();
            this.repaint();
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
           myTank.setDirection(1);
            myTank.moveRight();
           this.repaint();
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            myTank.setDirection(2);
            myTank.moveDown();
           this.repaint();
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            myTank.setDirection(3);
            myTank.moveLeft();
            this.repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}